<!-- 融资融券交易总量 -->
<template>
  <div class="border box">
    <div class="head">
      <div class="type clear">
        <span
          class="item fl"
          :class="{ cur: market == 'all' }"
          @click="changeData('all')"
          >两市合计图</span
        >
        <span
          class="item fl"
          :class="{ cur: market == 'sh' }"
          @click="changeData('sh')"
          >沪市图</span
        >
        <span
          class="item fl"
          :class="{ cur: market == 'sz' }"
          @click="changeData('sz')"
          >深市图</span
        >
      </div>
      <span class="title">融资融券交易总量</span>
    </div>
    <div class="tag clear">
      <span class="label fl">数据区间</span>
      <span class="item fl" :class="{ cur: day == 1 }" @click="day = 1"
        >1日</span
      >
      <span class="item fl" :class="{ cur: day == 3 }" @click="day = 3"
        >3日</span
      >
      <span class="item fl" :class="{ cur: day == 5 }" @click="day = 5"
        >5日</span
      >
      <span class="item fl" :class="{ cur: day == 10 }" @click="day = 10"
        >10日</span
      >
    </div>
    <div v-if="day == 1" class="types">
      <div class="flex grow">
        <div class="flex grow">
          <div class="flex column grow">
            <div class="flex grow">
              <span
                :class="{ active: type == 'rzrqye' }"
                @click="type = 'rzrqye'"
                ><i class="icon"></i
                ><span class="line green1"></span>融资融券余额(元)</span
              >
            </div>
            <div class="flex grow">
              <span
                :class="{ active: type == 'rzrqyecz' }"
                @click="type = 'rzrqyecz'"
                ><i class="icon"></i
                ><span class="line blue1"></span>融资融券余额差值(元)</span
              >
            </div>
          </div>
        </div>
        <div class="flex grow">
          <div class="flex grow column">
            <div class="flex grow">
              <span :class="{ active: type == 'rzye' }" @click="type = 'rzye'"
                ><i class="icon"></i
                ><span class="line green2"></span>融资余额(元)</span
              >
            </div>
            <div class="flex grow">
              <span :class="{ active: type == 'rqye' }" @click="type = 'rqye'"
                ><i class="icon"></i
                ><span class="line yellow"></span>融券余额(元)</span
              >
            </div>
          </div>
        </div>
        <div class="flex grow">
          <div class="flex grow column">
            <div class="flex grow">
              <span :class="{ active: type == 'rzjme' }" @click="type = 'rzjme'"
                ><i class="icon"></i
                ><span class="pillar purple1"></span>融资净买入(元)</span
              >
            </div>
            <div class="flex grow">
              <span :class="{ active: type == 'rqmcl' }" @click="type = 'rqmcl'"
                ><i class="icon"></i
                ><span class="pillar blue2"></span>融券净卖出(股)</span
              >
            </div>
          </div>
        </div>
        <div class="flex grow">
          <div class="flex grow column">
            <div class="flex grow">
              <span
                :class="{ active: type == 'rzyezb' }"
                @click="type = 'rzyezb'"
                ><i class="icon"></i
                ><span class="line purple2"></span>融资余额占流通市值比(%)</span
              >
            </div>
            <div class="flex grow">
              <span class="nocur last"
                ><span class="line red"></span>{{ suffix }}</span
              >
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-if="day == 3" class="types">
      <div class="flex grow">
        <div class="flex grow">
          <div class="flex column grow">
            <div class="flex grow">
              <span
                :class="{ active: type == 'rqjmg_threed' }"
                @click="type = 'rqjmg_threed'"
                ><i class="icon"></i
                ><span class="pillar green1"></span>3日融券净卖出(股)</span
              >
            </div>
            <div class="flex grow">
              <span
                :class="{ active: type == 'rzjme_threed' }"
                @click="type = 'rzjme_threed'"
                ><i class="icon"></i
                ><span class="pillar blue1"></span>3日融资净买入(元)</span
              >
            </div>
          </div>
        </div>
        <div class="flex grow">
          <div class="flex column grow">
            <div class="flex grow">
              <span
                :class="{ active: type == 'rzmre_threed' }"
                @click="type = 'rzmre_threed'"
                ><i class="icon"></i
                ><span class="pillar green2"></span>3日融资买入额(元)</span
              >
            </div>
            <div class="flex grow">
              <span
                :class="{ active: type == 'rqmcl_threed' }"
                @click="type = 'rqmcl_threed'"
                ><i class="icon"></i
                ><span class="pillar yellow"></span>3日融券卖出量(股)</span
              >
            </div>
          </div>
        </div>
        <div class="flex grow">
          <div class="flex column grow">
            <div class="flex grow">
              <span
                :class="{ active: type == 'rzche_threed' }"
                @click="type = 'rzche_threed'"
                ><i class="icon"></i
                ><span class="pillar purple1"></span>3日融资偿还额(元)</span
              >
            </div>
            <div class="flex grow">
              <span
                :class="{ active: type == 'rqchl_threed' }"
                @click="type = 'rqchl_threed'"
                ><i class="icon"></i
                ><span class="pillar blue2"></span>3日融券偿还量(股)</span
              >
            </div>
          </div>
        </div>
        <div class="flex grow">
          <div class="flex column grow">
            <div class="flex grow">
              <span :class="{ active: type == 'sh300' }" @click="type = 'sh300'"
                ><i class="icon"></i
                ><span class="pillar purple2"></span>收盘沪深300</span
              >
            </div>
            <div class="flex grow">
              <span class="nocur last"
                ><span class="pillar orange"></span
                ><span class="pillar green3"></span>{{ suffix }}</span
              >
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-if="day == 5" class="types">
      <div class="flex grow">
        <div class="flex grow">
          <div class="flex column grow">
            <div class="flex grow">
              <span
                :class="{ active: type == 'rqjmg_fived' }"
                @click="type = 'rqjmg_fived'"
                ><i class="icon"></i
                ><span class="pillar green1"></span>5日融券净卖出(股)</span
              >
            </div>
            <div class="flex grow">
              <span
                :class="{ active: type == 'rzjme_fived' }"
                @click="type = 'rzjme_fived'"
                ><i class="icon"></i
                ><span class="pillar blue1"></span>5日融资净买入(元)</span
              >
            </div>
          </div>
        </div>
        <div class="flex grow">
          <div class="flex column grow">
            <div class="flex grow">
              <span
                :class="{ active: type == 'rzmre_fived' }"
                @click="type = 'rzmre_fived'"
                ><i class="icon"></i
                ><span class="pillar green2"></span>5日融资买入额(元)</span
              >
            </div>
            <div class="flex grow">
              <span
                :class="{ active: type == 'rqmcl_fived' }"
                @click="type = 'rqmcl_fived'"
                ><i class="icon"></i
                ><span class="pillar yellow"></span>5日融券卖出量(股)</span
              >
            </div>
          </div>
        </div>
        <div class="flex grow">
          <div class="flex column grow">
            <div class="flex grow">
              <span
                :class="{ active: type == 'rzche_fived' }"
                @click="type = 'rzche_fived'"
                ><i class="icon"></i
                ><span class="pillar purple1"></span>5日融资偿还额(元)</span
              >
            </div>
            <div class="flex grow">
              <span
                :class="{ active: type == 'rqchl_fived' }"
                @click="type = 'rqchl_fived'"
                ><i class="icon"></i
                ><span class="pillar blue1"></span>5日融券偿还量(股)</span
              >
            </div>
          </div>
        </div>
        <div class="flex grow">
          <div class="flex column grow">
            <div class="flex grow">
              <span :class="{ active: type == 'sh300' }" @click="type = 'sh300'"
                ><i class="icon"></i
                ><span class="pillar purple2"></span>收盘沪深300</span
              >
            </div>
            <div class="flex grow">
              <span class="nocur last"
                ><span class="pillar orange"></span
                ><span class="pillar green3"></span>{{ suffix }}</span
              >
            </div>
          </div>
        </div>
      </div>
    </div>
    <div v-if="day == 10" class="types">
      <div class="flex grow">
        <div class="flex grow">
          <div class="flex column grow">
            <div class="flex grow">
              <span
                :class="{ active: type == 'rqjmg_tend' }"
                @click="type = 'rqjmg_tend'"
                ><i class="icon"></i
                ><span class="pillar green1"></span>10日融券净卖出(股)</span
              >
            </div>
            <div class="flex grow">
              <span
                :class="{ active: type == 'rzjme_tend' }"
                @click="type = 'rzjme_tend'"
                ><i class="icon"></i
                ><span class="pillar blue1"></span>10日融资净买入(元)</span
              >
            </div>
          </div>
        </div>
        <div class="flex grow">
          <div class="flex column grow">
            <div class="flex grow">
              <span
                :class="{ active: type == 'rzmre_tend' }"
                @click="type = 'rzmre_tend'"
                ><i class="icon"></i
                ><span class="pillar green2"></span>10日融资买入额(元)</span
              >
            </div>
            <div class="flex grow">
              <span
                :class="{ active: type == 'rqmcl_tend' }"
                @click="type = 'rqmcl_tend'"
                ><i class="icon"></i
                ><span class="pillar yellow"></span>10日融券卖出量(股)</span
              >
            </div>
          </div>
        </div>
        <div class="flex grow">
          <div class="flex column grow">
            <div class="flex grow">
              <span
                :class="{ active: type == 'rzche_tend' }"
                @click="type = 'rzche_tend'"
                ><i class="icon"></i
                ><span class="pillar purple1"></span>10日融资偿还额(元)</span
              >
            </div>
            <div class="flex grow">
              <span
                :class="{ active: type == 'rqchl_tend' }"
                @click="type = 'rqchl_tend'"
                ><i class="icon"></i
                ><span class="pillar blue2"></span>10日融券偿还量(股)</span
              >
            </div>
          </div>
        </div>
        <div class="flex grow">
          <div class="flex column grow">
            <div class="flex grow">
              <span :class="{ active: type == 'sh300' }" @click="type = 'sh300'"
                ><i class="icon"></i
                ><span class="pillar purple2"></span>收盘沪深300</span
              >
            </div>
            <div class="flex grow">
              <span class="nocur last"
                ><span class="pillar orange"></span
                ><span class="pillar green3"></span>{{ suffix }}</span
              >
            </div>
          </div>
        </div>
      </div>
    </div>
    <div id="chartbox" style="height: 200px"></div>
  </div>
</template>

<script>
import { getChartList } from "../api/index";
import takeRight from "lodash/takeRight";
import dataHandler from "../script/dataHandler";
import { numberFormat } from "../script/utils";
import * as Highcharts from "highcharts";
import * as Exporting from "highcharts/modules/exporting";
import { fillZero } from "../script/fillZero";
Exporting(Highcharts);
const SuffixTable = {
  all1: "收盘-沪深300",
  sh1: "收盘-上证指数",
  sz1: "收盘-深证成指",

  all3: "3日涨跌幅(%)-沪深300",
  sh3: "3日涨跌幅(%)-上证指数",
  sz3: "3日涨跌幅(%)-深证成指",

  all5: "5日涨跌幅(%)-沪深300",
  sh5: "5日涨跌幅(%)-上证指数",
  sz5: "5日涨跌幅(%)-深证成指",

  all10: "10日涨跌幅(%)-沪深300",
  sh10: "10日涨跌幅(%)-上证指数",
  sz10: "10日涨跌幅(%)-深证成指",
};

export default {
  data() {
    return {
      market: "all",
      day: 1,
      type: "rzrqye",
      chartData: [],
      cacheType: { 1: "", 3: "", 5: "", 10: "" },
      defaultType: {
        1: "rzrqye",
        3: "rqjmg_threed",
        5: "rqjmg_fived",
        10: "rqjmg_tend",
      },
    };
  },

  components: {},

  computed: {
    suffix() {
      return SuffixTable[`${this.market}${this.day}`];
    },
  },

  async mounted() {
    await getChartList().then((back) => {
      if (back.status == 1) {
        let data = back.data;
        this.chartData = data.reverse();
        this.draw();
      }
    });
  },
  watch: {
    day(val) {
      // 选择缓存的类型, 如果没有使用默认值
      this.type = this.cacheType[this.day] || this.defaultType[this.day];
    },
    type(val) {
      this.cacheType[this.day] = val; // 保存选中的 type
      this.draw();
    },
  },
  methods: {
    draw() {
      const isOneDay = this.day == 1;
      const chartData = isOneDay
        ? this.chartData
        : takeRight(this.chartData, 60);

      //显示刻度的位置:
      // 3,5,10日间隔9天显示刻度
      // 1日间隔39天
      const tickPositioner = isOneDay
        ? Array.from({ length: 7 }, (_, i) => 239 - 39 * i)
        : Array.from({ length: 7 }, (_, i) => 59 - 9 * i);

      console.log("draw -> chartData", chartData);
      const datas = dataHandler(chartData, {
        day: this.day,
        type: this.type,
      });

      const self = this;
      console.log(datas);
      Highcharts.chart("chartbox", {
        chart: {
          spacing: [30, 30, 30, 30],
        },
        title: { text: "" },
        credits: { enabled: false },
        exporting: { enabled: false },
        legend: { enabled: false },
        xAxis: {
          tickPositioner: () => tickPositioner,
          labels: {
            align: "center",
            formatter() {
              return chartData[this.value].dim_date;
            },
          },
        },
        yAxis: [
          { title: "" },
          {
            title: "",
            opposite: true,
            labels: {
              align: "center",
              formatter() {
                return numberFormat(this.value);
              },
            },
          },
        ],
        series: datas,
        tooltip: {
          backgroundColor: "rgba(0, 0, 0, 0.7)",
          borderColor: "#666666",
          shared: true,
          formatter: function () {
            // 比较两个点的y轴坐标点的像素值
            const getPx = (point) => point.series.yAxis.toPixels(point.y);
            const isGreat = getPx(this.points[0]) - getPx(this.points[1]) >= 0;

            return this.points.reduce(function (prev, point) {
              // 如果@开头使用计算
              const name = /^@/.test(point.series.name)
                ? self.suffix
                : point.series.name;

              const a = `${prev}<br/>`;
              const b = `<span style="font-size:14px;color:${
                point.color
              }">${name}: ${numberFormat(point.y)}</span><br/>`;

              return isGreat ? b + a : a + b;
            }, `${chartData[this.x].dim_date}`);
          },
        },
        plotOptions: {
          column: {
            borderWidth: 0,
          },
          spline: {
            lineWidth: 1,
            marker: {
              enabled: false,
              symbol: "circle",
            },
            states: {
              hover: {
                lineWidth: 1,
              },
            },
          },
        },
      });
    },
    async changeData(market) {
      this.market = market;
      await getChartList({ marketType: market }).then((back) => {
        if (back.status == 1) {
          let data = back.data;
          this.chartData = data.reverse();
          this.draw();
        }
      });
    },
  },
};

// 主题颜色
Highcharts.theme = {
  chart: {
    backgroundColor: "#090910",
    plotBorderColor: "#606063",
  },
  xAxis: {
    gridLineColor: "#2C2C30",
    labels: {
      style: {
        color: "#E0E0E3",
      },
    },
    lineColor: "#2C2C30",
    minorGridLineColor: "#505053",
    tickColor: "#2C2C30",
  },
  yAxis: {
    gridLineColor: "#2C2C30",
    labels: {
      style: {
        color: "#E0E0E3",
      },
    },
    lineColor: "#2C2C30",
    minorGridLineColor: "#505053",
    tickColor: "#2C2C30",
    tickWidth: 1,
  },
  tooltip: {
    backgroundColor: "rgba(0, 0, 0, 0.85)",
    style: {
      color: "#F0F0F0",
    },
  },
  plotOptions: {
    series: {
      dataLabels: {
        color: "#F0F0F3",
        style: {
          fontSize: "13px",
        },
      },
      marker: {
        lineColor: "#333",
      },
    },
    boxplot: {
      fillColor: "#505053",
    },
  },
  labels: {
    style: {
      color: "#2C2C30",
    },
  },
};

Highcharts.setOptions(Highcharts.theme);
</script>
<style lang="scss" scoped>
@import "../style/var.scss";
.border {
  border: 2px solid $border;
  border-radius: 6px;
}
.box {
  background: $bg;
  margin-bottom: 14px;
  .head {
    background: $border;
    line-height: 34px;
    text-align: center;
    position: relative;
    .type {
      position: absolute;
      left: 0;
      top: 0;
      padding-left: 4px;
      padding-top: 5px;
      .item {
        height: 24px;
        line-height: 24px;
        color: #cccccc;
        font-size: 16px;
        padding: 0 8px;
        cursor: pointer;
        position: relative;
        .icon {
          position: absolute;
          display: inline-block;
          width: 27px;
          height: 15px;
          top: -2px;
          right: -14px;
          background: url($PIC_URL + "/tlbypcWeb/securities/new.png") no-repeat
            center;
        }
        &.cur {
          color: #ffffff;
          margin: 0 10px;
          background: #35497c;
          border-radius: 4px;
        }
      }
    }
    .title {
      font-size: 18px;
      color: #ff4e39;
      font-weight: 400;
    }
  }
  .tag {
    line-height: 28px;
    background: $border;
    border: 1px solid $bg;
    span {
      color: #b3b3b3;
      display: inline-block;
      font-size: 14px;
      border-right: 1px solid $bg;
      padding: 0 18px;
      &.cur {
        background: #35497c;
        color: #ffffff;
      }
      &.item {
        padding: 0 15px;
        cursor: pointer;
      }
    }
  }
  .types {
    color: #999999;
    font-size: 14px;
    line-height: 25px;
    padding-left: 47px;
    padding-top: 15px;
    width: 70%;
    span {
      cursor: pointer;
      height: 27px;
      .line {
        display: inline-block;
        width: 20px;
        height: 4px;
        position: relative;
        top: -3px;
        margin-right: 5px;
      }
      .pillar {
        display: inline-block;
        width: 10px;
        height: 10px;
        margin-right: 5px;
      }
      .green1 {
        background: #5ad119;
      }
      .green2 {
        background: #22ab37;
      }
      .green3 {
        background: #08751c;
      }
      .purple1 {
        background: #7d68f2;
      }
      .purple2 {
        background: #be37ff;
      }
      .blue1 {
        background: #004986;
      }
      .blue2 {
        background: #008fce;
      }
      .yellow {
        background: #ff8a00;
      }
      .orange {
        background: #fd4848;
      }
      .red {
        background: #ea2827;
      }
      .icon {
        display: inline-block;
        width: 14px;
        height: 14px;
        border-radius: 50%;
        border: 1px solid #999999;
        position: relative;
        top: 3px;
        margin-right: 6px;
      }
      &.active {
        color: #e6e6e6;
        .icon {
          border: 1px solid #ff4e39;
          position: relative;
          text-align: center;

          top: 6px;
          &::before {
            content: "";
            display: inline-block;
            width: 6px;
            height: 6px;
            border-radius: 50%;
            position: relative;
            top: -8px;
            background: #ff4e39;
          }
        }
      }
      &.last {
        padding-left: 22px;
      }
      &.nocur {
        cursor: auto;
      }
    }
  }
}
</style>
